#!/bin/bash

# SSH密码认证修复脚本
# 请在阿里云控制台通过VNC或Workbench登录服务器后运行此脚本

echo "正在修复SSH密码认证..."

# 备份原始配置文件
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup

# 修改SSH配置以允许密码认证
sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config
sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config

# 确保以下配置正确
echo "PasswordAuthentication yes" | sudo tee -a /etc/ssh/sshd_config
echo "PubkeyAuthentication yes" | sudo tee -a /etc/ssh/sshd_config
echo "PermitRootLogin yes" | sudo tee -a /etc/ssh/sshd_config

# 重启SSH服务
sudo systemctl restart ssh

# 检查SSH服务状态
sudo systemctl status ssh

echo "SSH密码认证已启用，请重新尝试连接"
echo "如果仍有问题，请检查防火墙设置："
echo "sudo ufw allow 22"
echo "sudo ufw reload"
